class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1:
            return s

        rows = [[] for _ in range(numRows)]
        current_row = 0
        direction = 1  # 1表示向下，-1表示向上

        for char in s:
            rows[current_row].append(char)
            current_row += direction
            # 检查是否需要反转方向
            if current_row == numRows or current_row == -1:
                direction *= -1
                current_row += 2 * direction  # 调整到正确的行

        # 合并所有行的字符
        result = ""
        for row in rows:
            result += "".join(row)

        return result